package com.woniu.supermarket.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.supermarket.entity.Imgs;
import com.woniu.supermarket.mapper.ImgsGoodsMapper;
import com.woniu.supermarket.mapper.ImgsMapper;
import com.woniu.supermarket.service.IImgsService;
import com.woniu.supermarket.vo.GoodsVo;
import com.woniu.supermarket.vo.ImgsGoodsDataVo;
import com.woniu.supermarket.vo.ImgsGoodsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ImgsServiceImpl implements IImgsService {

    @Autowired
    ImgsMapper imgsMapper;

    @Autowired
    ImgsGoodsMapper imgsGoodsMapper;

    @Override
    public int add(ImgsGoodsVo imgsGoodsVo) {
        int insert = imgsMapper.insert(imgsGoodsVo);
        return insert;
    }

    @Override
    public int remove(int id) {
        return imgsMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int removeBatchByIds(int[] ids) {
        int i = imgsMapper.deleteBatchImgs(ids);
        return 0;
    }

    @Override
    public int modify(ImgsGoodsVo imgsGoodsVo) {
        return imgsMapper.updateByPrimaryKey(imgsGoodsVo);
    }

    @Override
    public PageInfo<ImgsGoodsVo> queryByPage(Integer now, Integer size, String des, Integer typeId) {
        PageHelper.startPage(now,size);

        List<ImgsGoodsVo> lists = imgsMapper.selectImgsGoodsByCons(des,typeId);
        for(ImgsGoodsVo i : lists){
            System.out.println(i);
        }
        return new PageInfo<>(lists);
    }


    @Override
    public ImgsGoodsVo queryById(int id) {
        return imgsMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<ImgsGoodsDataVo> queryIGDV() {
        List<ImgsGoodsDataVo> list = new ArrayList<>();

        List<Imgs> imgs = imgsMapper.selectAll();
        for(Imgs img:imgs){
            ImgsGoodsDataVo imgsGoodsDataVo = new ImgsGoodsDataVo();

            imgsGoodsDataVo.setId(img.getId());
            imgsGoodsDataVo.setImgsrc(img.getImgName());
            imgsGoodsDataVo.setCid(img.getCategoryId());
            list.add(imgsGoodsDataVo);
        }

        return list;
    }

    @Override
    public int addIGDV(ImgsGoodsDataVo igdv) {
        return imgsGoodsMapper.insert(igdv);
    }

    @Override
    public void removeIGDV(int gid) {
        imgsGoodsMapper.deleteByGoodId(gid);
    }

    @Override
    public void removeBatchIGDVByIds(int[] ids) {
        imgsGoodsMapper.deleteBatchIGDVByIds(ids);
    }


}
